package com.tedu.springboot23022.controller;

import com.tedu.springboot23022.entity.Article;
import com.tedu.springboot23022.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {

    private static File ArticleDir;

    static {
        ArticleDir = new File("./Articles");
        if (!ArticleDir.exists()) {
            ArticleDir.mkdirs();
        }
    }

    @RequestMapping("/writeArticle")
    public void writeArticle(HttpServletRequest request, HttpServletResponse response) {
        String headline = request.getParameter("headline");
        String author = request.getParameter("author");
        String content = request.getParameter("content");

        System.out.println(headline + "," + author + "," + content);

        if (
                headline == null || headline.isEmpty() ||
                author == null || author.isEmpty() ||
                content == null || content.isEmpty()) {
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        Article article = new Article(headline, author, content);
        File file = new File(ArticleDir, headline + ".obj");

        if (file.exists()) {
            try {
                response.sendRedirect("/article_fail.html");
            } catch (IOException e) {
                e.printStackTrace();
            }
            return;
        }

        try (
                FileOutputStream fis = new FileOutputStream(file);
                ObjectOutputStream oos = new ObjectOutputStream(fis);
                ){
            oos.writeObject(article);

            response.sendRedirect("/article_success.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/articleList")
    public void writList(HttpServletRequest request, HttpServletResponse response) {

        List<Article> articleList = new ArrayList<>();
        File[] arts = ArticleDir.listFiles(a -> a.getName().endsWith(".obj"));

        for (File art : arts) {
            try (
                    FileInputStream fis = new FileInputStream(art);
                    ObjectInputStream ois = new ObjectInputStream(fis);
            ) {

                Article article = (Article) ois.readObject();
                articleList.add(article);

            } catch (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            }
        }


        try {
            response.setContentType("text/html;charset=utf-8");
            PrintWriter pw = response.getWriter();
            pw.println("<!DOCTYPE html>");
            pw.println("<html lang=\"en\">");
            pw.println("<head>");
            pw.println("<meta charset=\"UTF-8\">");
            pw.println("<title>Title</title>");
            pw.println("</head>");
            pw.println("<body>");
            pw.println("<center>");
            pw.println("<h1>文章内容</h1>");
            pw.println("<table border='1'>");
            pw.println("<tr>");
            pw.println("<td>标题</td>");
            pw.println("<td>作者</td>");
            pw.println("<td>内容</td>");
            pw.println("</tr>");

            for (Article article : articleList) {
                pw.println("<tr>");
                pw.println("<td>" + article.getHeadline() + "</td>");
                pw.println("<td>" + article.getAuthor() + "</td>");
                pw.println("<td>" + article.getContent() + "</td>");
                pw.println("</tr>");
            }
            pw.println("</table>");
            pw.println("</center>");
            pw.println("</body>");
            pw.println("</html>");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
